package com.zhilei.controller;

import com.zhilei.base.BaseInfoProperties;
import com.zhilei.grace.result.GraceJSONResult;
import com.zhilei.pojo.ResumeEducation;
import com.zhilei.pojo.ResumeProjectExp;
import com.zhilei.pojo.ResumeWorkExp;
import com.zhilei.pojo.bo.*;
import com.zhilei.pojo.vo.EditResumeExpectVO;
import com.zhilei.pojo.vo.ResumeVO;
import com.zhilei.service.*;
import com.zhilei.untils.GsonUtils;
import com.zhilei.untils.LocalDateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author 志磊
 */
@Slf4j
@RestController
@RequestMapping("resume")
public class ResumeController extends BaseInfoProperties {

    @Autowired
    private ResumeService resumeService;

    @Autowired
    private ResumeWorkExpService resumeWorkExpService;

    @Autowired
    private ResumeProjectExpService resumeProjectExpService;

    @Autowired
    private ResumeEducationService resumeEducationService;
    @Autowired
    private ResumeExpectService resumeExpectService;

    /**
     * 初始化简历
     *
     * @param userId
     * @return
     */
    @PostMapping("init")
    public GraceJSONResult init(@RequestParam("userId") String userId) {

        resumeService.initResume(userId);
        return GraceJSONResult.ok();

    }

    /**
     * 根据当前登录用户查询用户信息
     *
     * @param userId 用户id
     * @return
     */
    @PostMapping("queryMyResume")
    public GraceJSONResult queryMyResume(@RequestParam("userId") String userId) {
        if (StringUtils.isBlank(userId)) {
            return GraceJSONResult.error();
        }
        /*
          修改简历信息涉及到多张表的关联查询，并且修改的并不会很频繁
          所以采用redis进行查询
         */
        String resumeKey = REDIS_RESUME_EXPECT + ":" + userId;
        String s = redis.get(resumeKey);
        ResumeVO resume = null;

        if (StringUtils.isBlank(s)) {
            resume = resumeService.getResume(userId);
            redis.set(resumeKey, GsonUtils.object2String(resume), 60);
        } else {
            resume = GsonUtils.stringToBean(s, ResumeVO.class);
        }
        return GraceJSONResult.ok(resume);
    }

    /**
     * 修改在线简历信息
     *
     * @param editResumeBO 简历的bo类
     * @return
     */
    @PostMapping("modify")
    public GraceJSONResult queryMyResume(@RequestBody @Valid EditResumeBO editResumeBO) {
        resumeService.modify(editResumeBO);
        return GraceJSONResult.ok();
    }

    /**
     * 查询工作经验
     *
     * @param workExpId 经验表的id
     * @param userId    用户表的id
     * @return
     */
    @PostMapping("getWorkExp")
    public GraceJSONResult getWorkExp(@RequestParam("workExpId") String workExpId, @RequestParam("userId") String userId) {
        if (StringUtils.isBlank(workExpId) || StringUtils.isBlank(userId)) {
            return GraceJSONResult.error();
        }
        ResumeWorkExp workExp = resumeWorkExpService.getWorkExp(workExpId, userId);
        return GraceJSONResult.ok(workExp);
    }

    /**
     * 保存工作经验
     *
     * @param editWorkExpBO 保存工作经验bo类
     * @return
     */
    @PostMapping("editWorkExp")
    public GraceJSONResult editWorkExp(@RequestBody @Valid EditWorkExpBO editWorkExpBO) {
        resumeWorkExpService.editWorkExp(editWorkExpBO);
        return GraceJSONResult.ok();
    }

    /**
     * 删除工作经验
     *
     * @param workExpId 经验表的id
     * @param userId    用户表的id
     * @return
     */
    @PostMapping("deleteWorkExp")
    public GraceJSONResult deleteWorkExp(@RequestParam("workExpId") String workExpId, @RequestParam("userId") String userId) {
        if (StringUtils.isBlank(workExpId) || StringUtils.isBlank(userId)) {
            return GraceJSONResult.error();
        }
        resumeWorkExpService.deleteWorkExp(workExpId, userId);
        return GraceJSONResult.ok();
    }

    /**
     * 查询项目经验
     *
     * @param projectExpId 项目表的id
     * @param userId       用户id
     * @return
     */
    @PostMapping("getProjectExp")
    public GraceJSONResult getProjectExp(@RequestParam("projectExpId") String projectExpId, @RequestParam("userId") String userId) {
        if (StringUtils.isBlank(projectExpId) || StringUtils.isBlank(userId)) {
            return GraceJSONResult.error();
        }
        ResumeProjectExp projectExp = resumeProjectExpService.getProjectExp(projectExpId, userId);
        return GraceJSONResult.ok(projectExp);
    }

    /**
     * 保存、编辑项目经验
     *
     * @param editProjectExpBO 保存、编辑项目经验bo类
     * @return
     */
    @PostMapping("editProjectExp")
    public GraceJSONResult editProjectExp(@RequestBody @Valid EditProjectExpBO editProjectExpBO) {
        resumeProjectExpService.editProjectExp(editProjectExpBO);
        return GraceJSONResult.ok();
    }

    /**
     * 删除项目经验
     *
     * @param projectExpId 经验表的id
     * @param userId       用户表的id
     * @return
     */
    @PostMapping("deleteProjectExp")
    public GraceJSONResult deleteProjectExp(@RequestParam("projectExpId") String projectExpId, @RequestParam("userId") String userId) {
        if (StringUtils.isBlank(projectExpId) || StringUtils.isBlank(userId)) {
            return GraceJSONResult.error();
        }
        resumeProjectExpService.deleteProjectExp(projectExpId, userId);
        return GraceJSONResult.ok();
    }

    /**
     * 删除教育经历
     *
     * @param eduId  教育表的id
     * @param userId 用户表的id
     * @return
     */
    @PostMapping("deleteEducation")
    public GraceJSONResult deleteEducation(@RequestParam("eduId") String eduId, @RequestParam("userId") String userId) {
        if (StringUtils.isBlank(eduId) || StringUtils.isBlank(userId)) {
            return GraceJSONResult.error();
        }
        resumeEducationService.deleteResumeEducation(eduId, userId);
        return GraceJSONResult.ok();
    }

    /**
     * 查询教育经历
     *
     * @param eduId  教育表的id
     * @param userId 用户表的id
     * @return
     */
    @PostMapping("getEducation")
    public GraceJSONResult getEducation(@RequestParam("eduId") String eduId, @RequestParam("userId") String userId) {
        if (StringUtils.isBlank(eduId) || StringUtils.isBlank(userId)) {
            return GraceJSONResult.error();
        }
        ResumeEducation resumeEducation = resumeEducationService.getResumeEducation(eduId, userId);
        return GraceJSONResult.ok(resumeEducation);
    }

    /**
     * 保存、编辑教育经历
     *
     * @param editEducationBO 保存教育经历bo类
     * @return
     */
    @PostMapping("editEducation")
    public GraceJSONResult editEducation(@RequestBody @Valid EditEducationBO editEducationBO) {
        resumeEducationService.resumeEducation(editEducationBO);
        return GraceJSONResult.ok();
    }

    /**
     * 保存编辑求职期望
     *
     * @param editEducationBO 求职期望实体类
     * @return
     */
    @PostMapping("editJobExpect")
    public GraceJSONResult editJobExpect(@RequestBody @Valid EditResumeExpectBO editEducationBO) {
        resumeExpectService.editJobExpect(editEducationBO);
        return GraceJSONResult.ok();
    }

    /**
     * 删除求职期望
     *
     * @param resumeExpectId 简历期望id
     * @param userId         用户id
     * @return
     */
    @PostMapping("deleteMyResumeExpect")
    public GraceJSONResult deleteMyResumeExpect(@RequestParam("resumeExpectId") String resumeExpectId, @RequestParam("userId") String userId) {
        if (StringUtils.isBlank(resumeExpectId) || StringUtils.isBlank(userId)) {
            return GraceJSONResult.error();
        }
        resumeExpectService.deleteMyResumeExpect(resumeExpectId, userId);
        return GraceJSONResult.ok();
    }

    /**
     * 根据期望得id查询期望信息
     *
     * @param resumeId 简历id
     * @param userId   用户id
     * @return
     */
    @PostMapping("getMyResumeExpectList")
    public GraceJSONResult getMyResumeExpectList(@RequestParam("resumeId") String resumeId, @RequestParam("userId") String userId) {
        if (StringUtils.isBlank(resumeId)) {
            return GraceJSONResult.error();
        }
        List<EditResumeExpectVO> editResumeExpectVO = resumeExpectService.getMyResumeExpectList(resumeId, userId);
        return GraceJSONResult.ok(editResumeExpectVO);
    }

    /**
     * 根据期望得id查询期望信息
     *
     * @param resumeId 简历id
     * @param userId   用户id
     * @return
     */
    @PostMapping("refresh")
    public GraceJSONResult refresh(@RequestParam("userId") String userId, @RequestParam("resumeId") String resumeId) {
        if (StringUtils.isBlank(resumeId) || StringUtils.isBlank(resumeId)) {
            return GraceJSONResult.error();
        }

        // TODO 设置简历刷新的最大值
        int maxRefreshTimes = 3;
        // TODO 设置简历刷新的初始值
        int refreshResume = 1;
        // TODO 获取当前日期的字符串
        String today = LocalDateUtils.getLocalDateStr();
        // TODO 在redis中去查询当前简历存在的刷新次数，如果存在 <= 最大的刷新次数就刷新
        // TODO 否则就提示已超过最大的刷新次数
        String userAlreadyRefreshedCounts = redis.get(USER_ALREADY_REFRESHED_COUNTS + ":" + today + ":" + userId);

        if (StringUtils.isBlank(userAlreadyRefreshedCounts)) {
            // TODO 如果当前在redis中查询的数据为0，那么表示当前时间内用户还没刷新过简历，所以设置24小时过期时间
            redis.set(USER_ALREADY_REFRESHED_COUNTS + ":" + today + ":" + userId, refreshResume + "", 24 * 60 * 60);
        } else {
            // TODO 那就代表用户已经刷新过了简历，更新简历刷新的次数就可以
            refreshResume = Integer.parseInt(userAlreadyRefreshedCounts);
        }

        // TODO 如果查询出来的次数 > 设置的次数报错就完事了
        if (refreshResume > maxRefreshTimes) {
            return GraceJSONResult.errorMsg("您已超过了刷新简历的最大次数，请明日再来");
        } else {
            // TODO 如果还没超过，那么就在redis中加一次，并且返回成功
            resumeService.refresh(resumeId, userId);
            redis.increment(USER_ALREADY_REFRESHED_COUNTS + ":" + today + ":" + userId, 1);
        }
        return GraceJSONResult.ok();
    }
}














































